import { login } from "@/api/user"
// import { getUserInfo } from "../../api/user"
const state = {
    pass: "",
    username: "",
    user_id: "",

}


const mutations = {
    SET_USERNAME(state, payload) {
        state.username = payload
    },
    SET_USER_ID(state, payload) {
        state.user_id = payload
    },
    DEL_USER_ID(state) {
        state.user_id = ""

    },

}


const actions = {
    async login({ commit }, userInfo) {
        let res = await login(userInfo);
        // console.log(res);

        if (res.data.length) {
            //设置用户名
            commit("SET_USERNAME", res.data[0].username)
            commit("SET_USER_ID", res.data[0].id)
                //登陆成功
            return {
                code: 200,
                msg: "登陆成功",
                data: res.data[0]
            }
        } else {
            return {
                code: 201,
                msg: "用户名或密码错误",
                data: {}
            }
        }
    },
    //    async editor({commit},userInfo){
    //        let{id,phone,img,password} = userInfo
    //     try{
    //         let res = await editor(id,{phone,img,password})
    //         commit("SET_PHONE",res.data.phone)
    //         commit("SET_IMAGE",res.data.img)
    //         return res
    //         // console.log(res);

    //     }catch(err){


    //     }
    //     },
    //     async getUserInfo({commit},id){
    //      try{
    //          let res = await getUserInfo(id)
    //          commit("SET_PHONE",res.data.phone)
    //          commit("SET_IMAGE",res.data.img)
    //          return res
    //          // console.log(res);

    //      }catch(err){


    //      }
    //      },
}

export default {
    state,
    mutations,
    actions
}